package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.HasPermi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.dao.SysConfig;
import com.ruoyi.system.domain.persist.SysConfigPo;
import com.ruoyi.system.domain.query.SysConfigQo;
import com.ruoyi.system.domain.view.SysConfigVo;
import com.ruoyi.system.domain.wrapper.query.SysConfigWrapperQ;
import com.ruoyi.system.domain.wrapper.view.SysConfigWrapperV;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 参数配置Controller
 *
 * @author 尹国春
 * @date 2022-07-25
 */
@Api(value = "参数配置", tags = {"参数配置"})
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController
{
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 参数配置分页查询
     */
    @ApiOperation(value="参数配置分页查询",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("system:config:list")
    @GetMapping("/list")
    public TableDataInfo<SysConfigVo> list(SysConfigQo sysConfig)
    {
        IPage<SysConfig> page=sysConfigService.page(sysConfig.getPage(),SysConfigWrapperQ.getQueryWrapper(sysConfig));
        return SysConfigWrapperV.build().pageVO(page);
    }

    /**
     * 参数配置导出
     */
    @ApiOperation("参数配置导出")
    @HasPermi("system:config:list")
    @Log(title = "参数配置导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigQo sysConfig)
    {
        List<SysConfig> list = sysConfigService.list(SysConfigWrapperQ.getQueryWrapper(sysConfig));
        List<SysConfigVo> voList = SysConfigWrapperV.build().listVO(list);
        ExcelUtil<SysConfigVo> util = new ExcelUtil<>(SysConfigVo.class);
        util.exportExcel(response, voList, "参数配置数据");
    }

    /**
     * 获取参数配置详细信息
     */
    @ApiOperation("参数配置信息")
    @HasPermi("system:config:list")
    @GetMapping(value = "/get/{configKey}")
    public AjaxResult getInfo(@PathVariable("configKey") String configKey)
    {
        SysConfig entity=sysConfigService.getById(configKey);
        return AjaxResult.success(SysConfigWrapperV.build().entityVO(entity));
    }

    /**
     * 新增参数配置
     */
    @ApiOperation("参数配置新增")
    @HasPermi("system:config:edit")
    @Log(title = "新增参数配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysConfigPo sysConfig)
    {
        return toAjax(sysConfigService.insert(sysConfig));
    }

    /**
     * 修改参数配置
     */
    @ApiOperation("参数配置修改")
    @HasPermi("system:config:edit")
    @Log(title = "修改参数配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysConfigPo sysConfig)
    {
        return toAjax(sysConfigService.updateById(sysConfig));
    }

    /**
     * 删除参数配置
     */
    @ApiOperation("参数配置删除")
    @HasPermi("system:config:edit")
    @Log(title = "删除参数配置", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{configKeys}")
    public AjaxResult remove(@PathVariable String[] configKeys)
    {
        return toAjax(sysConfigService.removeByIds(Arrays.asList(configKeys)));
    }
}
